package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.bo.wxcouponbo.WxCouponBo;
import com.cskaoyan.bean.bo.wxcouponbo.WxCouponListBo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.BaseRespVo2;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.service.wx.WxCouponService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @BelongsProject: market-project
 * @BelongsPackage: com.cskaoyan.controller.wx
 * @CreateTime: 2022-11-22  14:04
 * @Description: TODO
 * @Version: 1.0
 */
@RestController
@RequestMapping("wx/coupon")
public class WxCoupon {
    @Autowired
    WxCouponService wxCouponService;


    @RequestMapping("mylist")
    public BaseRespVo WxCouponMyList(Short status, Integer page, Integer limit) {

        PageInfo<WxCouponListBo> list = wxCouponService.select(status, page, limit);
        CommonData data = CommonData.data(list);
        return BaseRespVo.succeed(data);
    }

    @RequestMapping("list")
    public BaseRespVo WxCouponList(Integer page, Integer limit) {
        PageInfo<WxCouponBo> list = wxCouponService.selectCoupon(page, limit);
        CommonData data = CommonData.data(list);
        return BaseRespVo.succeed(data);
    }

    @RequestMapping("receive")
    public BaseRespVo2 WxCouponReceive(@RequestBody Map map) {
        Integer cid = (Integer) map.get("couponId");
        Integer i = wxCouponService.insertUserByCid(cid);
        if (i == 2) {
            return BaseRespVo2.fail("领取达上限");
        } else if (i == 1) {
            return BaseRespVo2.fail("已发放完毕");
        }

        return BaseRespVo2.succeed();
    }

    @RequestMapping("exchange")
    public BaseRespVo WxCouponExchange(@RequestBody Map map) {
        String code = (String) map.get("code");
        wxCouponService.insertByCode(code);
        return BaseRespVo.succeed();

    }

    @RequestMapping("selectlist")
    public BaseRespVo WxCouponSelectlist(Integer cartId,Integer grouponRulesId) {
        PageInfo<WxCouponListBo> list = wxCouponService.selectCart(cartId,grouponRulesId);
        CommonData data = CommonData.data(list);
        return BaseRespVo.succeed(data);
    }



}